<?
	require_once("../cBasic/950cons.php");

	$output = array();
	
	$thisreqid = $_GET['ReqID'];
		
	$emplist_skilled=array();
	$sQuery="select  emp_skill.EmpID from req_skill, emp_skill where emp_skill.SkillID = req_skill.SkillID AND req_skill.ReqID = ".$thisreqid." group by emp_skill.EmpID order by emp_skill.EmpID";
	$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
	while ( $aRow = mysql_fetch_array( $rResult ) ){
		$rec=array();
		$rec[]= $aRow['EmpID'];
		$emplist_skilled[] = $rec;
	}
	
	$rstartdate;
	$renddate;
	$sQuery="select StartDate, EndDate from request where ReqID = ".$thisreqid;
	$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
	while ( $aRow = mysql_fetch_array( $rResult ) ){
		$rstartdate =str_replace('-','',$aRow['StartDate']);
		$renddate =str_replace('-','',$aRow['EndDate']);
	}
	
	$req= array();
	$emp= array();
	$sQuery="select EmpID, ReqID from contract";
	$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
	while ( $aRow = mysql_fetch_array( $rResult ) ){
		$req[]=$aRow['ReqID'];
		$emp[]=$aRow['EmpID'];
	}
	
	$emplist_ignore=array();
	for ($i=0;$i<count($req);$i++){
		$sQuery="select ReqID, StartDate, EndDate from request where ReqID = '".$req[$i]."'";
		$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
		while ( $aRow = mysql_fetch_array( $rResult ) ){
			$rec=array();
			$startdate =str_replace('-','',$aRow['StartDate']);
			$enddate =str_replace('-','',$aRow['EndDate']);
			if (($rstartdate>=$startdate && $rstartdate<=$enddate) || ($renddate>=$startdate && $renddate<=$enddate)){
				$rec[]= $emp[$i];
				$rec[]= $aRow['StartDate'];
				$rec[]= $aRow['EndDate'];
				$emplist_ignore[]=$rec;
			}
		}
	}

	for ($x=0;$x<count($emplist_skilled);$x++){
		for ($y=0;$y<count($emplist_ignore);$y++){
			if ($emplist_skilled[$x][0]==$emplist_ignore[$y][0]){
				array_splice($emplist_skilled, $x, 1);
			}
			//break;
		}
	}
	
if (count($emplist_skilled)>0){	
	for ($i=0;$i<count($emplist_skilled);$i++){
		$sQuery="select EmpFName, EmpLName from employee where EmpID = ".$emplist_skilled[$i][0];
		$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
		while ( $aRow = mysql_fetch_array( $rResult ) ){
			$emplist_skilled[$i][] = $aRow['EmpFName'];
			$emplist_skilled[$i][] = $aRow['EmpLName'];
		}
	}	
}

for ($i=0;$i<count($emplist_skilled);$i++){
	$sQuery="select SkillID from emp_skill where SkillID in (select SkillID from req_skill where ReqID = ".$thisreqid.") and EmpID = ".$emplist_skilled[$i][0];
	$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
	while ( $aRow = mysql_fetch_array( $rResult ) ){
		$emplist_skilled[$i][3][]=$aRow['SkillID'];
	}
}
for ($x=0;$x<count($emplist_skilled);$x++){
	for ($y=0;$y<count($emplist_skilled[$x][3]);$y++){
		$sQuery="select skName from skill where skID = ".$emplist_skilled[$x][3][$y];
		$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
		while ( $aRow = mysql_fetch_array( $rResult ) ){
			$emplist_skilled[$x][4][]=$aRow['skName'];
		}
	}
}

if (count($emplist_ignore)>0){	
	for ($i=0;$i<count($emplist_ignore);$i++){
		$sQuery="select EmpFName, EmpLName from employee where EmpID = ".$emplist_ignore[$i][0];
		$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
		while ( $aRow = mysql_fetch_array( $rResult ) ){
			$emplist_ignore[$i][] = $aRow['EmpFName'];
			$emplist_ignore[$i][] = $aRow['EmpLName'];
		}
	}
}

	$sQuery="select EmpAmount from request where ReqID = ".$thisreqid;
	$rResult = mysql_query( $sQuery, $con ) or die(mysql_error());
	while ( $aRow = mysql_fetch_array( $rResult ) ){
		$output['empamount']=$aRow['EmpAmount'];
	}
	$output['skilled'] = $emplist_skilled;
	$output['ignored'] = $emplist_ignore;
	
	echo json_encode($output);
?>